1 package org.apache.maven.surefire.testng.conf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.maven.surefire.booter.ProviderParameterNames;
28 import org.apache.maven.surefire.testset.TestSetFailedException;
29 import org.testng.TestNG;
30 import org.testng.xml.XmlSuite;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class TestNGMapConfigurator
46 implements Configurator
47 {
48 public void configure( TestNG testng, Map<String, String> options )
49 throws TestSetFailedException
50 {
51 Map convertedOptions = getConvertedOptions( options );
52 testng.configure( convertedOptions );
53 }
54
55 public void configure( XmlSuite suite, Map<String, String> options )
56 throws TestSetFailedException
57 {
58 String threadCountString = options.get( ProviderParameterNames.THREADCOUNT_PROP );
59 int threadCount = ( null != threadCountString ) ? Integer.parseInt( threadCountString ) : 1;
60 suite.setThreadCount( threadCount );
61
62 String parallel = options.get( ProviderParameterNames.PARALLEL_PROP );
63 if ( parallel != null )
64 {
65 suite.setParallel( parallel );
66 }
67 }
68
69 Map<String, Object> getConvertedOptions( Map<String, String> options )
70 throws TestSetFailedException
71 {
72 Map<String, Object> convertedOptions = new HashMap<String, Object>();
73 convertedOptions.put( "-mixed", false );
74 for ( Map.Entry<String, String> entry : options.entrySet() )
75 {
76 String key = entry.getKey();
77 Object val = entry.getValue();
78 if ( "listener".equals( key ) )
79 {
80 val = AbstractDirectConfigurator.loadListenerClasses( entry.getValue() );
81 }
82 else if ( "objectfactory".equals( key ) )
83 {
84 val = AbstractDirectConfigurator.loadClass( entry.getValue() );
85 }
86 else if ( "testrunfactory".equals( key ) )
87 {
88 val = AbstractDirectConfigurator.loadClass( entry.getValue() );
89 }
90 else if ( "reporter".equals( key ) )
91 {
92
93 val = convertReporterConfig( val );
94 key = "reporterslist";
95 }
96 else if ( "junit".equals( key ) )
97 {
98 val = convert( val, Boolean.class );
99 }
100 else if ( "skipfailedinvocationcounts".equals( key ) )
101 {
102 val = convert( val, Boolean.class );
103 }
104 else if ( "mixed".equals( key ) )
105 {
106 val = convert( val, Boolean.class );
107 }
108 else if ( "configfailurepolicy".equals( key ) )
109 {
110 val = convert( val, String.class );
111 }
112 else if ( "group-by-instances".equals( key ) )
113 {
114 val = convert( val, Boolean.class );
115 }
116 else if ( ProviderParameterNames.THREADCOUNT_PROP.equals( key ) )
117 {
118 val = convert ( val, String.class );
119 }
120 else if ( "suitethreadpoolsize".equals( key ) )
121 {
122
123 val = convert( val, Integer.class );
124 }
125 else if ( "dataproviderthreadcount".equals( key ) )
126 {
127
128 val = convert( val, Integer.class );
129 }
130
131 if ( key.startsWith( "-" ) )
132 {
133 convertedOptions.put( key, val );
134 }
135 else
136 {
137 convertedOptions.put( "-" + key, val );
138 }
139 }
140 return convertedOptions;
141 }
142
143
144 private Object convertReporterConfig( Object val )
145 {
146 final String reporterConfigClassName = "org.testng.ReporterConfig";
147 try
148 {
149 Class<?> reporterConfig = Class.forName( reporterConfigClassName );
150 Method deserialize = reporterConfig.getMethod( "deserialize", String.class );
151 Object rc = deserialize.invoke( null, val );
152 ArrayList<Object> reportersList = new ArrayList<Object>();
153 reportersList.add( rc );
154 return reportersList;
155 }
156 catch ( Exception e )
157 {
158 return val;
159 }
160 }
161
162 protected Object convert( Object val, Class<?> type )
163 {
164 if ( val == null )
165 {
166 return null;
167 }
168 if ( type.isAssignableFrom( val.getClass() ) )
169 {
170 return val;
171 }
172
173 if ( ( type == Boolean.class || type == boolean.class ) && val.getClass() == String.class )
174 {
175 return Boolean.valueOf( (String) val );
176 }
177
178 if ( ( type == Integer.class || type == int.class ) && val.getClass() == String.class )
179 {
180 return Integer.valueOf( (String) val );
181 }
182
183 if ( type == String.class )
184 {
185 return val.toString();
186 }
187
188 return val;
189 }
190 }